const Router = require("koa-router");
const R = require("../app/R");

const userDao = require("../dao/UserDao");
const blogDao = require("../dao/BlogDao");

const BlogRouter = new Router({ prefix: "/blog" });

BlogRouter.post("/save", async (ctx, next) => {
  const data = ctx.request.body;

  const result = await blogDao.save(data);
  ctx.body = new R().setResult({
    Blog: result,
  });
});

BlogRouter.post("/getUserInfo", async (ctx, next) => {
  const data = ctx.request.body;
  if (data.UserId) {
    const result = await userDao.getOneById(data.UserId);
    ctx.body = new R().setResult({
      User: result,
    });
  }
  if (data.PhoneNumber) {
    const result = await userDao.getOneByPhoneNumber(data.PhoneNumber);
    ctx.body = new R().setResult({
      User: result,
    });
  }
});

BlogRouter.post("/getUserBaseInfo", async (ctx, next) => {
  const data = ctx.request.body;
  const result = await userDao.getUserBaseInfo(data.UserId);
  ctx.body = new R().setResult({
    User: result,
  });
});

BlogRouter.post("/updataUserBaseInfo", async (ctx, next) => {
  const data = ctx.request.body;
  console.log(data);
  const result = await userDao.update(data);

  ctx.body = new R().setResult({
    User: result,
  });
});

module.exports = BlogRouter;
